#!/bin/bash
set -e

# 识别系统，设置 sed -i 参数
if sed --version >/dev/null 2>&1; then
  # GNU sed (Linux)
  SED_INPLACE=(-i)
else
  # BSD sed (macOS)
  SED_INPLACE=(-i '')
fi


# 路径变量
ANDROID_DIR="/workspace/android"
UNIAPP_DIR="/workspace/uniapp"
UNIAPP_ANDROID_RESOURCE="$UNIAPP_DIR/android"
APP_ID="__UNI__2461B0E"
APP_DIR="$ANDROID_DIR/app"
APP_DIST_DIR="$UNIAPP_DIR/dist/build/app"
MANIFEST="$APP_DIST_DIR/manifest.json"
ANDROID_MANIFEST="$APP_DIR/src/main/AndroidManifest.xml"
STRINGS_XML="$APP_DIR/src/main/res/values/strings.xml"
DCLOUD_CONTROL_XML="$APP_DIR/src/main/assets/data/dcloud_control.xml"
BUILD_GRADLE="$APP_DIR/build.gradle"
KEYSTORE_NAME="key.keystore"
DCLOUD_APPKEY="d1f8813b02f749f166316f8d3d76efb8"


echo "获取最新安卓项目代码"
cd $ANDROID_DIR
git reset --hard HEAD
git clean -fd
git pull

echo "构建 uniapp 项目"
cd $UNIAPP_DIR
yarn build:app


echo "读取 manifest.json ..."
VERSION_CODE=$(jq -r '.version.code' "$MANIFEST")
VERSION_NAME=$(jq -r '.version.name' "$MANIFEST")
APP_NAME=$(jq -r '.name' "$MANIFEST")

echo "appid: $APP_ID"
echo "versionCode: $VERSION_CODE"
echo "versionName: $VERSION_NAME"
echo "appName: $APP_NAME"

if [ -z "$APP_ID" ] || [ -z "$VERSION_CODE" ] || [ -z "$VERSION_NAME" ] || [ -z "$APP_NAME" ]; then
  echo "从 manifest.json 读取到空值，退出"
  exit 1
fi

# 替换 根目录下的build.gradle
echo "修改 $ANDROID_DIR/build.gradle ..."
cp -f $UNIAPP_ANDROID_RESOURCE/gradle/build-root.gradle $ANDROID_DIR/build.gradle


# 替换build.gradle
echo "修改 $BUILD_GRADLE ..."
cp -f $UNIAPP_ANDROID_RESOURCE/gradle/build.gradle $BUILD_GRADLE


# 修改 versionCode
sed "${SED_INPLACE[@]}" "/defaultConfig {/,/}/ s/versionCode[[:space:]]*[0-9][0-9]*/versionCode $VERSION_CODE/" "$BUILD_GRADLE"

# 修改 versionName
sed "${SED_INPLACE[@]}" "/defaultConfig {/,/}/ s/versionName[[:space:]]*\"[^\"]*\"/versionName \"$VERSION_NAME\"/" "$BUILD_GRADLE"


echo "修改 $ANDROID_MANIFEST dcloud_appkey..."
xmlstarlet ed -L -u '/manifest/application/meta-data[@android:name="dcloud_appkey"]/@android:value' -v "$DCLOUD_APPKEY" $ANDROID_MANIFEST

echo "修改 $STRINGS_XML 应用名称..."
xmlstarlet ed -L -u '/resources/string[@name="app_name"]' -v "$APP_NAME" $STRINGS_XML

echo "修改 $DCLOUD_CONTROL_XML 中 appid"
xmlstarlet ed -L -u '/hbuilder/apps/app/@appid' -v "$APP_ID" "$DCLOUD_CONTROL_XML"

echo "创建资源目录..."

mkdir -p $APP_DIR/src/main/assets/apps/${APP_ID}/www

echo "复制uniapp 到 $APP_DIR/src/main/assets/apps/${APP_ID}/www..."

cp -r $APP_DIST_DIR/* $APP_DIR/src/main/assets/apps/${APP_ID}/www


echo "复制证书到 $APP_DIR/key.keystore..."

cp $UNIAPP_ANDROID_RESOURCE/key.keystore $APP_DIR/key.keystore

echo "应用图标 icon.png"
cp $UNIAPP_ANDROID_RESOURCE/icon.png $APP_DIR/src/main/res/drawable-xxhdpi/icon.png


echo "修改gradle 镜像源地址 gradle.gradle-wrapper.properties"
sed "${SED_INPLACE[@]}" 's|distributionUrl=https\\://services.gradle.org/distributions/gradle-\([0-9.]*\)-bin.zip|distributionUrl=https\\://mirrors.cloud.tencent.com/gradle/gradle-\1-bin.zip|g' "$ANDROID_DIR/gradle/wrapper/gradle-wrapper.properties"


cd $ANDROID_DIR

echo "gradlew 添加可执行权限..."

chmod +x ./gradlew

echo "构建gradle clean..."

# 构建gradle
./gradlew clean --no-daemon --stacktrace --info

echo "构建assembleRelease apk..."

# 构建apk
./gradlew assembleRelease --no-daemon --stacktrace --info

# apk 目录 app/build/outputs/apk/release/app-release.apk
echo "构建完成, apk 目录: $ANDROID_DIR/app/build/outputs/apk/release/app-release.apk"

echo "上传 apk 到minio"


# 测试 mc 是否可用
mc --version

# 配置minio 别名
mc alias set hnyxc http://192.168.1.3:9010 minioadmin 3onnak9xaidfsrvr

# 文件名 + 时间
FILE_NAME="${APP_NAME}-${VERSION_NAME}-$(date +%Y%m%d%H%M%S).apk"
# 上传 apk 到minio
mc cp $ANDROID_DIR/app/build/outputs/apk/release/app-release.apk hnyxc/hospital-kiosk/应用安装包/${FILE_NAME}

echo "上传完成"

echo "minio 地址: http://192.168.1.3:9010/hospital-kiosk/应用安装包/${FILE_NAME}"
